<?php
namespace App\Http\Middleware;

use App\Exceptions\PermissionException;
use App\Services\MenuService;
use App\Services\PermissionService;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

class PermissionCheck
{
    /**
     * 权限验证
     * @param Request $request
     * @param Closure $next
     * @return mixed
     * @throws PermissionException
     */
    public function handle(Request $request, Closure $next)
    {
        $user = Auth::user();

        // 演示模式校验
        if (!$user->sys && config('app.demo_model') && Request()->method() != 'GET') {
            throw new PermissionException(__('common.app_demo_model_tips'));
        }

        // 菜单权限校验
        if (!PermissionService::menuCheck()) {
            throw new PermissionException();
        }
        // 栏目权限校验
        if ($request->info_category) {
            if (!PermissionService::cateCheck($request->info_category)) {
                throw new PermissionException();
            }
        }
        return $next($request);
    }
}
